package com.zjb.demo.framework.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

/**
 * @author 庄佳彬
 * @since 2023/12/27 14:02
 */
public class YseOrNoConverter implements Converter<String> {

    @Override
    public Class<?> supportJavaTypeKey() {
        return String.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public String convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        var value = cellData.getStringValue();
        return switch (value) {
            case "是", "1", "YES", "Y", "yse", "y" -> "1";
            case "否", "0", "NO", "N", "no", "n" -> "0";
            default -> value;
        };
    }

    @Override
    public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        var data = switch (value){
            case "是", "1", "YES", "Y", "yse", "y"  -> "是";
            case "否", "0", "NO", "N", "no", "n" -> "否";
            default -> value;
        };
        return new WriteCellData<>(data);
    }
}
